package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Logistics;
import com.ruoyi.system.service.ILogisticsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * 单据Controller
 *
 * @author ruoyi
 * @date 2024-03-26
 */
@Controller
@RequestMapping("/system/logistics")
public class LogisticsController extends BaseController {
    private String prefix = "system/logistics";

    @Autowired
    private ILogisticsService logisticsService;

    @RequiresPermissions("system:logistics:view")
    @GetMapping()
    public String logistics() {
        return prefix + "/logistics";
    }

    /**
     * 查询单据列表
     */
    @RequiresPermissions("system:logistics:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Logistics logistics) {
        startPage();
        List<Logistics> list = logisticsService.selectLogisticsList(logistics);
        return getDataTable(list);
    }

    /**
     * 导出单据列表
     */
    @RequiresPermissions("system:logistics:export")
    @Log(title = "单据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Logistics logistics) {
        List<Logistics> list = logisticsService.selectLogisticsList(logistics);
        ExcelUtil<Logistics> util = new ExcelUtil<Logistics>(Logistics.class);
        return util.exportExcel(list, "单据数据");
    }

    /**
     * 新增单据
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存单据
     */
    @RequiresPermissions("system:logistics:add")
    @Log(title = "单据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Logistics logistics) {
        return toAjax(logisticsService.insertLogistics(logistics));
    }

    /**
     * 修改单据
     */
    @RequiresPermissions("system:logistics:sign")
    @GetMapping("/sign/{id}")
    public String sign(@PathVariable("id") Long id, ModelMap mmap) {
        Logistics logistics = logisticsService.selectLogisticsById(id);
        mmap.put("logistics", logistics);
        return prefix + "/sign";
    }

    /**
     * 签收保存单据
     */
    @RequiresPermissions("system:logistics:sign")
    @Log(title = "单据", businessType = BusinessType.UPDATE)
    @PostMapping("/sign")
    @ResponseBody
    public AjaxResult signSave(@RequestParam("files") MultipartFile[] files,
                               @RequestParam("id") Long id,
                               @RequestParam("signTime") Date signTime,
                               @RequestParam("remark") String remark) {
        System.out.println(id);
        System.out.println(signTime);
        System.out.println(remark);
        for (MultipartFile file : files) {
            System.out.println(file);
        }
        return AjaxResult.success();
    }

    /**
     * 删除单据
     */
    @RequiresPermissions("system:logistics:remove")
    @Log(title = "单据", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(logisticsService.deleteLogisticsByIds(ids));
    }
}
